package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Division;
import com.sl.au.entity.EventType;
import com.sl.au.repository.EventTypeRepository;
import com.sl.au.service.EventTypeService;

@Service("EventTypeService")
@Transactional
public class EventTypeServiceImpl extends BaseServiceImpl implements EventTypeService {

	@Autowired
	EventTypeRepository eventTypeRepository;

	@Override
	public EventTypeRepository getRepository() {
		return this.eventTypeRepository;
	}

	@Override
	public List<Map<String, Object>> getNodeMap(List<EventType> node) {
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		for (EventType p : node) {
			if (null != p) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("parentId", p.getEventParent());

				map.put("name", String.valueOf((p.getEventOrder()) / 2) + p.getEventName());
				if (StringUtils.hasText(p.getEventParent())) {
					map.put("isParent", p.getEventParent());
				}
				map.put("level", p.getEventLevel().getExtended1());
				map.put("open", true);
				String iconAddress = p.getIconAddress();
				if (iconAddress == null || iconAddress.equals("")) {
					iconAddress = "/wcsi/img/defaultEventType.png";
				} else {
					iconAddress = "/wcsi/" + iconAddress;
				}
				map.put("icon", iconAddress);
				nodeMap.add(map);
			}
		}
		return nodeMap;
	}

	@Override
	public List<EventType> getAllNodes() {
		List<EventType> nodes = new ArrayList<EventType>();
		nodes = eventTypeRepository.findAllNodes();
		return nodes;
	}

	/*********** 获取未绑定节点 *************/
	@Override
	public List<EventType> getEventTypeByKey(int start, int length, String key, String orderName, String dir) {
		Page<EventType> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (key != null && org.springframework.util.StringUtils.hasText(key)) {
			pages = eventTypeRepository.getEventTypeByKey(key, pageable);
		} else {
			pages = eventTypeRepository.getEventType(pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getEventTypeByKeyCount(String key) {
		long count = 0;
		if (key != null && org.springframework.util.StringUtils.hasText(key)) {
			count = eventTypeRepository.getEventTypeByKeyCount(key);
		} else {
			count = eventTypeRepository.getEventTypeCount();
		}
		return count;
	}

	/************ 获取子节点 ************/

	@Override
	public List<EventType> getChildrenEventTypeByKey(int start, int length, String id, String key, String orderName,
			String dir) {
		Page<EventType> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (StringUtils.hasText(key)) {
			pages = eventTypeRepository.findByeventParentAndKey(id, key, pageable);
		} else {
			pages = eventTypeRepository.findByEventParent(id, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getChildrenEventTypeByKeyCount(String id, String key) {
		int count = 0;
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			count = eventTypeRepository.findByeventParentAndKey(id, key, pageable).getContent().size();
		} else {
			count = eventTypeRepository.findByEventParent(id, pageable).getContent().size();
		}
		return count;
	}

	/**********************************/
	@Override
	public void deleteNodes(String[] ids) {
		if (ids.length > 0) {
			for (String id : ids) {
				if (StringUtils.hasText(id)) {
					eventTypeRepository.setEventType(id);
				}
			}
		}
	}

	@Override
	public void setEventTypeOrder(String groupParent) {
		List<EventType> EventTypes = eventTypeRepository.getEventTypesByParent(groupParent);
		if (EventTypes != null) {
			EventType eventType = null;
			for (int i = 0; i < EventTypes.size(); i++) {
				eventType = EventTypes.get(i);
				eventType.setEventOrder(i * 2);
				eventTypeRepository.save(eventType);
			}
		}
	}

	@Override
	public String getEventTypeIdentify(String id) {
		EventType eventType = eventTypeRepository.findOne(id);
		String identify = eventType.getEventIdentify();
		String parent = eventType.getEventParent();
		if (!"0".equals(parent)) {
			identify = this.getEventTypeIdentify(parent) + identify;
		}
		return identify;
	}

	@Override
	public List<EventType> findOuterTypeOnlyCon(int start, int length, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeOnlyCon(pageable, con).getContent();
	}

	@Override
	public int findOuterTypeCountOnlyCon(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeOnlyCon(pageable, con).getContent().size();
	}

	@Override
	public List<EventType> findOuterTypeNone(int start, int length) {

		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeNone(pageable).getContent();
	}

	@Override
	public int findOuterTypeCountNone() {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeNone(pageable).getContent().size();
	}

	@Override
	public List<EventType> findOuterTypeByKeyAndCon(int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeByKeyAndCon(pageable, key, con).getContent();
	}

	@Override
	public int findOuterTypeCountByKeyAndCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeByKeyAndCon(pageable, key, con).getContent().size();
	}

	@Override
	public List<EventType> findOuterTypeOnlyKey(int start, int length, String key) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeOnlyKey(pageable, key).getContent();
	}

	@Override
	public int findOuterTypeCountOnlyKey(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findOuterTypeOnlyKey(pageable, key).getContent().size();
	}

	@Override
	public List<EventType> findInnerTypeOnlyCon(int start, int length, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeOnlyCon(pageable, con).getContent();
	}

	@Override
	public int findInnerTypeCountOnlyCon(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeOnlyCon(pageable, con).getContent().size();
	}

	@Override
	public List<EventType> findInnerTypeNone(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeNone(pageable).getContent();
	}

	@Override
	public int findInnerTypeCountNone() {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeNone(pageable).getContent().size();
	}

	@Override
	public int findInnerTypeCountByKeyAndCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeCountByKeyAndCon(pageable, key, con).getContent().size();
	}

	@Override
	public List<EventType> findInnerTypeByKeyAndCon(int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeCountByKeyAndCon(pageable, key, con).getContent();
	}

	@Override
	public List<EventType> findInnerTypeOnlyKey(int start, int length, String key) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeOnlyKey(pageable, key).getContent();
	}

	@Override
	public int findInnerTypeCountOnlyKey(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventTypeRepository.findInnerTypeOnlyKey(pageable, key).getContent().size();
	}

	@Override
	public List<EventType> findByParentId(String parentId) {
		
		return eventTypeRepository.findByParentId(parentId);
	}

}
